package mapreduce;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import warningreportcount.ReportCount;

import java.io.IOException;
import java.util.Iterator;

public class ReportCountReducer extends Reducer<Text, ReportCount, Text, ReportCount> {
    private ReportCount reportCount = new ReportCount();

    protected void reduce(Text key, Iterable<ReportCount> values,Context context) throws IOException, InterruptedException {
        Iterator<ReportCount> iterator = values.iterator();
        while (iterator.hasNext()) {
            reportCount.setCount(reportCount.getCount() + iterator.next().getCount());
        }
        String error = key.toString().split("-")[1];
        reportCount.setError(error);
        try {
            context.write(key, reportCount);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
